package com.dhcc.sds.standard.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.standard.dao.SdsStandDicCompStaMapper;
import com.dhcc.sds.standard.dto.SdsStandDicCompDto;
import com.dhcc.sds.standard.dto.SdsStandStaDto;
import com.dhcc.sds.standard.service.ISdsStandDicCompStaService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("sdsStandDicCpmpStaService")
public class SdsStandDicCompStaServiceImpl extends BaseServiceImpl<SdsStandDicCompStaMapper, SdsStandStaDto> implements ISdsStandDicCompStaService {

    @Override
    public List<SdsStandStaDto> page(Page<SdsStandStaDto> page, Map<String, Object> params) {
        List<SdsStandStaDto> staDtos = this.baseMapper.page(page, params);
        List<SdsStandStaDto> result = new ArrayList<>();
        for (SdsStandStaDto staDto : staDtos) {
            Long id = staDto.getId();
//            List<SdsStandStaDto> dtoList = this.baseMapper.selectCountByID(id);
            //标准字典匹配数量 有匹配到的就算
            Integer dicNumOfMatches = this.baseMapper.selectDicNumOfMatchesByID(id);
            if (dicNumOfMatches == null) {
                dicNumOfMatches = 0;
            }
            //上报直接使用数量 不包括值域字典配置的规则，值域的使用放在规则库使用次数中统一统计的
            Integer fillNumOfMatches = this.baseMapper.selectFillNumOfMatchesByID(id);
            if (fillNumOfMatches == null) {
                fillNumOfMatches = 0;
            }
            //规则库使用次数
            Integer ruleNumOfUse = this.baseMapper.selectRuleNumOfUseByID(id);
            if (ruleNumOfUse == null) {
                ruleNumOfUse = 0;
            }
            //标准字典数量
//            Integer dicNum = dtoList.size();
            Integer dicNum = this.baseMapper.selecDicNumByID(id);
            if (dicNum == null) {
                dicNum = 0;
            }
//            for (SdsStandStaDto dto : dtoList) {
//                dicNumOfMatches += dto.getDicNumOfMatches();
//                fillNumOfMatches += dto.getFillNumOfMatches();
//                ruleNumOfUse += dto.getRuleNumOfUse();
//                dicNum++;
//            }
            staDto.setDicNum(dicNum);
            staDto.setDicNumOfMatches(dicNumOfMatches);
            staDto.setFillNumOfMatches(fillNumOfMatches);
            staDto.setRuleNumOfUse(ruleNumOfUse);
            result.add(staDto);
        }
        return result;
    }

    @Override
    public List<SdsStandDicCompDto> dictMatchPage(Page<SdsStandDicCompDto> page, Map<String, Object> params) {
        List<SdsStandDicCompDto> result = new ArrayList<>();
        if ("matched".equals(params.get("type"))) {
            // 标准化字典已匹配
            result = this.baseMapper.selectDicOfMatches(page, params);
            for (SdsStandDicCompDto sdsStandDicCompDto : result) {
                if (CommonUtil.isEmpty(sdsStandDicCompDto.getDescription())) {
                    if (CommonUtil.isNotEmpty(sdsStandDicCompDto.getLabOrderDescription())) {
                        sdsStandDicCompDto.setDescription(sdsStandDicCompDto.getLabOrderDescription());
                    } else if (CommonUtil.isNotEmpty(sdsStandDicCompDto.getLabItemDescription())) {
                        sdsStandDicCompDto.setDescription(sdsStandDicCompDto.getLabItemDescription());
                    } else if (CommonUtil.isNotEmpty(sdsStandDicCompDto.getSubTitle())) {
                        sdsStandDicCompDto.setDescription(sdsStandDicCompDto.getSubTitle());
                    }
                }
            }
        } else if ("noMatch".equals(params.get("type"))) {
            // 标准化字典未匹配
            result = this.baseMapper.selectDicOfNoMatchesByID(page, params);
        } else if ("all".equals(params.get("type"))) {
            // 标准化字典全部
            result = this.baseMapper.selectDicOfAllMatchesByID(page, params);
            for (SdsStandDicCompDto sdsStandDicCompDto : result) {
                if (CommonUtil.isEmpty(sdsStandDicCompDto.getDescription())) {
                    if (CommonUtil.isNotEmpty(sdsStandDicCompDto.getLabOrderDescription())) {
                        sdsStandDicCompDto.setDescription(sdsStandDicCompDto.getLabOrderDescription());
                    } else if (CommonUtil.isNotEmpty(sdsStandDicCompDto.getLabItemDescription())) {
                        sdsStandDicCompDto.setDescription(sdsStandDicCompDto.getLabItemDescription());
                    } else if (CommonUtil.isNotEmpty(sdsStandDicCompDto.getSubTitle())) {
                        sdsStandDicCompDto.setDescription(sdsStandDicCompDto.getSubTitle());
                    }
                }
            }
        } else if ("fill".equals(params.get("type"))) {
            // 上报直接使用情况
            result = this.baseMapper.selectFillOfMatchesByID(page, params);
        } else if ("rule".equals(params.get("type"))) {
            // 规则库使用情况
            result = this.baseMapper.selectRuleOfUseByID(page, params);
        }
        return result;
    }
}
